package com.hrrt.home.admin.controller.app;

import com.hrrt.common.base.BaseResult;
import com.hrrt.common.base.Tip;
import com.hrrt.home.dao.model.*;
import com.hrrt.home.rpc.api.*;
import com.hrrt.home.socket.utils.SocketUtil;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

import static com.alibaba.dubbo.monitor.MonitorService.SUCCESS;

@Controller
@RequestMapping("/app/")
@Api(value = "app接口", description = "app接口")
public class HomeAppController {

    @Autowired
    private HomeRoomService homeRoomService;

    @Autowired
    private HomeEquipmentService homeEquipmentService;

    @Autowired
    private HomeBaseUnitService homeBaseUnitService;

    @Autowired
    private HomeBaseDeviceService homeBaseDeviceService;

    @Autowired
    private HomeGatewayService homeGatewayService;

    @Autowired
    private HomeButtonTypeService homeButtonTypeService;

    private static Logger _log = LoggerFactory.getLogger(HomeAppController.class);

//    @RequestMapping(value = "roomList", method = RequestMethod.GET)
//    @ResponseBody
//    public Tip roomList(String id) {
//        Tip tip = new Tip();
//        try {
//            HomeRoomExample homeRoomExample = new HomeRoomExample();
//            homeRoomExample.createCriteria().andSuiteIdEqualTo(id);
//            List<HomeRoom> homeRooms = homeRoomService.selectByExample(homeRoomExample);
//            tip = new Tip(homeRooms);
//        } catch (Exception e) {
//            tip = new Tip(1, "查询异常");
//        }
//        return tip;
//    }

    @RequestMapping(value = "equipmentList", method = RequestMethod.GET)
    @ResponseBody
    public List<HomeEquipment> equipmentList(int id) {
        HomeEquipmentExample homeEquipmentExample = new HomeEquipmentExample();
        homeEquipmentExample.createCriteria().andRoomIdEqualTo(id);
        List<HomeEquipment> homeEquipments = homeEquipmentService.selectByExample(homeEquipmentExample);
        return homeEquipments;
    }

    @RequestMapping(value = "equipmentListByUserId", method = RequestMethod.GET)
    @ResponseBody
    public Tip equipmentListByUserId(String id) {
        Tip tip;
        try {
//            HomeRoomExample homeRoomExample = new HomeRoomExample();
//            homeRoomExample.createCriteria().andUserIdEqualTo(id);
//            List<HomeRoom> homeRooms = homeRoomService.selectByExample(homeRoomExample);
//            List<HomeEquipment> list = new ArrayList<>();
//            for(HomeRoom h : homeRooms) {
//                HomeEquipmentExample  homeEquipmentExample = new HomeEquipmentExample();
//                homeEquipmentExample.createCriteria().andRoomIdEqualTo(h.getId());
//                List<HomeEquipment> homeEquipments = homeEquipmentService.selectByExample(homeEquipmentExample);
//                for(HomeEquipment he : homeEquipments) {
//                    list.add(he);
//                }
//
//            }
//            tip = new Tip(list);
            tip = new Tip();
        } catch (Exception e) {
            tip = new Tip("数据异常");
        }
        return tip;
    }

    @RequestMapping(value = "sendCommannd")
    @ResponseBody
    public Tip sendCommannd(int id, String command, int num) {
        Tip tip;
        HomeEquipmentExample homeEquipmentExample = new HomeEquipmentExample();
        homeEquipmentExample.createCriteria().andRoomIdEqualTo(id);
        HomeEquipment homeEquipment = homeEquipmentService.selectByPrimaryKey(id);
        if(homeEquipment != null) {
            HomeRoom homeRoom = homeRoomService.selectByPrimaryKey(homeEquipment.getRoomId());
            HomeBaseUnit homeBaseUnit = homeBaseUnitService.selectByPrimaryKey(homeRoom.getRoomBasicId());
            HomeBaseDevice homeBaseDevice = homeBaseDeviceService.selectByPrimaryKey(homeEquipment.getEquipBasicId());
            HomeGateway homeGateway = homeGatewayService.selectByPrimaryKey(homeRoom.getGatewayId());
            System.out.println(String.valueOf(homeRoom.getGatewayId()) + "," + homeBaseUnit.getPid() + "," + delX(homeBaseDevice.getPid()) + "," + judgeCommand(command, num));
            SocketUtil.socketTaskMap.get("87905509005909590990").openOrClose(homeGateway.getGatewayNum(), homeBaseUnit.getPid(), delX(homeBaseDevice.getPid()), judgeCommand(command, num));
            tip = new Tip();
        } else {
            tip = new Tip(1, "未查找到设备" );
        }
        return tip;
    }

    private int delX(String id) {
        return Integer.parseInt(id.split("0x")[1]);
    }

    private String judgeCommand(String commannd, int num) {
        String str;
        if(num != 100) {
            str = "12345678901234567890123456temp" + String.valueOf(num);
        } else if(commannd == "on" || "on".equals(commannd)) {
            str = "12345678901234567890123456tempon";
        } else {
            str = "1234567890123456789012345tempoff";
        }
        return str;
    }

    /**
     * 根据设备类型查询所有相关按钮进行学习
     * @param typeId
     * @return
     */
    @RequestMapping(value = "baseButton")
    @ResponseBody
    public Tip baseButton(Integer typeId) {
        Tip tip;
        try {
            HomeButtonTypeExample homeButtonTypeExample = new HomeButtonTypeExample();
            homeButtonTypeExample.createCriteria().andDeviceTypeIdEqualTo(typeId);
            List<HomeButtonType> list = homeButtonTypeService.selectByExample(homeButtonTypeExample);
            tip = new Tip(list);
        } catch (Exception e) {
            tip = new Tip(0);
        }

        return tip;
    }

}
